package com.yf.dialup;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class FileUtil {

	public static boolean saveUser(User user) {
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
					"data/data/com.yf.helloandroid/password.txt"));
			oos.writeObject(user);
			oos.close();

			return true;
		} catch (Exception e) {
			e.printStackTrace();

			return false;
		}
	}

	public static boolean saveUserByContext(Context context, User user) {
		try {
			// String filename = context.getFilesDir().getAbsolutePath() +
			// "/password.txt";
			// Log.v("FileUtil", filename);
			// ObjectOutputStream oos = new ObjectOutputStream(new
			// FileOutputStream(filename));
			ObjectOutputStream oos = new ObjectOutputStream(
					context.openFileOutput("password.txt", Context.MODE_PRIVATE));
			oos.writeObject(user);
			oos.close();

			return true;
		} catch (Exception e) {
			e.printStackTrace();

			return false;
		}
	}

	

	public static boolean saveUser2SD(Context context, User user) {
		try {
			// String filename = "/storage/sdcard/password.txt";
			String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/password.txt";
			Log.v("FileUtil", filename);
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
			oos.writeObject(user);
			oos.close();

			return true;
		} catch (Exception e) {
			e.printStackTrace();

			return false;
		}
	}

	public static User loadUserByContext(Context context) {
		try {
			// String filename = context.getFilesDir().getAbsolutePath() +
			// "/password.txt";
			// ObjectInputStream ois = new ObjectInputStream(new
			// FileInputStream(filename));
			ObjectInputStream ois = new ObjectInputStream(context.openFileInput("password.txt"));
			User user = (User) ois.readObject();
			ois.close();

			return user;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static User loadUserFromSD(Context context) {
		try {
			String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/password.txt";
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
			User user = (User) ois.readObject();
			ois.close();

			return user;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}
	
	public static boolean saveUserByContextWithWorld(Context context, String msg) {
		try {
			// String filename = context.getFilesDir().getAbsolutePath() +
			// "/password.txt";
			// Log.v("FileUtil", filename);
			// ObjectOutputStream oos = new ObjectOutputStream(new
			// FileOutputStream(filename));
			BufferedOutputStream bos = new BufferedOutputStream(context.openFileOutput("msg.txt",
					Context.MODE_WORLD_READABLE));
			bos.write(msg.getBytes());
			bos.close();
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();

			return false;
		}
	}

	public static String loadMsgByPath(String path) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(path));
			String msg = br.readLine();
			br.close();

			return msg;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

}
